package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> list() {
        //发生异常
//        int i = 10/0;
        return deptMapper.selectAll();
    }
    /**
     * springboot封装的restful请求方式
     * @GetMapping => @RequestMapping(method=RequestMethod.GET)
     * @PostMapping = > @RequestMapping(method=RequestMethod.POST)
     * @PutMapping => @RequestMapping(method=RequestMethod.PUT)
     * @DeleteMapping => @RequestMapping(method=RequestMethod.DELETE)
     */
    // restful，请求方式 操作类型
    // 限制请求方式
    //method属性：代表请求方式,可以有多个
    //value：请求地址
    //@RequestMapping(value = "/depts",method = {RequestMethod.GET})//接收请求
    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public void add(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(dept.getCreateTime());
        deptMapper.insert(dept);
    }

    @Override
    public void update(@RequestBody Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.selectById(id);
    }
}
